rm(list = ls())

library(ggthemes)
library(lubridate)
library(estimatr)
library(texreg)
library(tidyverse)

df = read_csv("data/ukraine_final.csv")

# DATA PREP ----------------------------------------------------------------
#defining controls to convert to factors
cols = c("ethnicity", "language", "religion", "sex", "region", "job")


df = df %>% 
  mutate(pcr_il = recode(pcr_il, "Very Unfavorable" = 0, "Unfavorable" = 1, "Neutral" = 2,
                         "Favorable" = 3, "Very Favorable" = 4, .default = NA_real_),
         pcr_ul = recode(pcr_ul, "Very Unfavorable" = 0, "Unfavorable" = 1, "Neutral" = 2,
                         "Favorable" = 3, "Very Favorable" = 4, .default =  NA_real_),
         pcr_par = recode(pcr_par, "Very Unfavorable" = 0, "Unfavorable" = 1, "Neutral" = 2,
                          "Favorable" = 3, "Very Favorable" = 4, .default =  NA_real_),
         
         pcr_il_p = recode(pcr_il_p, "Definitely not" = 0, "Probably not" = 1, "Probably yes" = 2,
                           "Definitely yes" = 3, .default =  NA_real_),
         pcr_il_j = recode(pcr_il_j, "Definitely not" = 0, "Probably not" = 1, "Probably yes" = 2,
                           "Definitely yes" = 3, .default =  NA_real_),
         
         pcr_ul_p = recode(pcr_ul_p, "Definitely not" = 0, "Probably not" = 1, "Probably yes" = 2,
                           "Definitely yes" = 3, .default =  NA_real_),
         pcr_ul_j = recode(pcr_ul_j, "Definitely not" = 0, "Probably not" = 1, "Probably yes" = 2,
                           "Definitely yes" = 3, .default =  NA_real_),
         
         pcr_par_p = recode(pcr_par_p, "Definitely not" = 0, "Probably not" = 1, "Probably yes" = 2,
                            "Definitely yes" = 3, .default =  NA_real_),
         pcr_par_j = recode(pcr_par_j, "Definitely not" = 0, "Probably not" = 1, "Probably yes" = 2,
                            "Definitely yes" = 3, .default =  NA_real_),
         
         pcr_mod = recode(pcr_mod, "Strongly Disapprove" = 0, "Disapprove" = 1, "Neutral" = 2, 
                          "Approve" = 3, "Strongly Approve" = 4, .default = NA_real_),
         
         region = recode(region, "The territory currently known as the Donetsk and Luhansk People Republics" = "LNR/DNR",
                         "The parts of Luhansk or Donetsk Oblast that are currently controlled by the Ukrainian government." = "East",
                         "Any other territory of Ukraine." = "West"),
         
         educ = recode(educ, "Primary (less than 7 years) complete or incomplete" = 0,
                       "Incomplete secondary (less than 10 years)" = 1,
                       "PTU without a secondary education (vocational elementary education)" = 2,
                       "General secondary" = 3,
                       "PTU with a secondary education (vocational secondary education)" = 4,
                       "Special technical" = 5, "Incomplete higher" = 6, 
                       "Higher (diploma of bachelor, specialist or master degree)" = 7, 
                       .default = NA_real_),
         
         income = recode(income, "Less than 1000 hryvnia" = 0, "1000-2000 hryvnia" = 1,
                         "2000-3000 hryvina" = 2, "3000-4000 hryvnia" = 3, "4000-5000 hryvnia" = 4,
                         "5000-6000 hryvnia" = 5, "6000-7000 hryvnia" = 6, "7000-8000 hryvnia" = 7,
                         "More than 8000 hryvnia" = 8, .default = NA_real_),
         age = as.numeric(age),
         nationalist = ifelse(ethnicity %in% c("I feel more Ukrainian than Russian", "I feel only Ukrainian"), 1, 0)) %>% 
  mutate_at(.vars = cols, .funs = funs(factor))


df$income2 = cut(df$income, 
                 breaks = quantile(df$income, probs = c(0, .33, .67, 1), na.rm = T), 
                 labels = c("Up ot 4000 hryvnia", "Up ot 7000 hryvnia", "Over 7000 hryvnia"), 
                 include.lowest = T)

df$religion2 = recode(df$religion, "Greek Catholic Church" = "Other", "Muslim (Islam)" = "Other",
                      "Protestant" = "Other", "Roman Catholic" = "Other", 
                      "Ukrainian Autocephalous Orthodox Church" = "Other",
                      "Religious but do not belong to a certain religion or church" = "Religious but no church")

df$job2 = recode(df$job, "Entrepreneur, farmer" = "Other", "Military servant" = "Other",
                 "Pension (because of age or disability)" = "Pension", 
                 "Professional (with higher education)" = "Professional", 
                 "Self employed businesswomen/men" = "Self-employed",
                 "Servant (without higher education)" = "Servant (no higher ed.)")

df$university = ifelse(df$educ == 7, 1,
                       ifelse(df$educ < 7, 0, NA))

# Controls ----------------------------------------------------------------
df$russian_eth = ifelse(df$ethnicity %in% c("I feel more Russian than Ukrainian", "I feel only Russian"), 1,
                        ifelse(df$ethnicity %in% c("I feel equally Ukrainian and Russian", "I feel more Ukrainian than Russian",
                                                   "I feel only Ukrainian", "I feel neither Russian nor Ukrainian"), 0, NA))
df$dfr_eth = ifelse(df$ethnicity %in% c("I feel more Ukrainian than Russian", "I feel only Ukrainian"), 1,
                    ifelse(df$ethnicity %in% c("I feel equally Ukrainian and Russian", "I feel more Russian than Ukrainian",
                                               "I feel only Russian", "I feel neither Russian nor Ukrainian"), 0, NA))

df$both_eth = ifelse(df$ethnicity %in% c("I feel equally Ukrainian and Russian"), 1,
                     ifelse(df$ethnicity %in% c("I feel equally Ukrainian and Russian", "I feel more Russian than Ukrainian",
                                                "I feel only Russian", "I feel neither Russian nor Ukrainian",
                                                "I feel more Ukrainian than Russian", "I feel only Ukrainian"), 0, NA))

df$russian_lang = ifelse(df$language %in% c("I speak both, but mostly Russian", "I speak Russian"), 1, 0)

df$employed = ifelse(df$job2 %in% c("Professional", "Self-employed", "Worker, farmworker"), 1, 0)

df$orthodox_rlgn = ifelse(df$religion2 %in% c("Ukrainian Orthodox Church", "Ukrainian Orthodox Church (Moscow Patriarchate)",
                                              "Ukrainian Orthodox Church (Kyiv Patriarchate)"), 1, 0)

df$other_rlgn = ifelse(df$religion2 %in% c("Other", "Religious but no church",
                                           "Unbeliever, atheist"), 1, 0)



#Prep for hypotheses ------------------------------------------------------
#ingroup vs. outgroup
df = df %>% 
  mutate(ingroup = ifelse((pcr_mod %in% c(3,4) & t_pcr == "separatists")|(pcr_mod %in% c(0,1) & t_pcr == "govt"), 1, 0), #ingroup includes supporters of either that received a treatment telling them their group was responsible Note I'm including both because of those who received seps/gov EQUALLY responsible
         outgroup = ifelse((pcr_mod %in% c(3,4) & t_pcr == "govt")|(pcr_mod %in% c(0,1) & t_pcr == "separatists"), 1, 0))
         

df = df %>% mutate(in_out = ifelse(outgroup == 1, "outgroup", ifelse(ingroup == 1, "ingroup", "control")),
              in_out = factor(in_out, levels = c("control", "ingroup", "outgroup")))


#revenge
df = df %>% mutate(gov_kill = recode(progov_fam, "Yes" = 1, "No" = 0, .default = NA_real_),
                   sep_kill = recode(sep_fam, "Yes" = 1, "No" = 0, .defatul = NA_real_),
                   fam_kill = ifelse(gov_kill == 1 | sep_kill == 1, 1,
                                     ifelse(gov_kill == 0 & sep_kill == 0, 0, NA)))


# Support by region -------------------------------------------------------

df %>% 
  select(ResponseId, pcr_il, pcr_ul, region) %>% 
  pivot_longer(-c(ResponseId, region)) %>% 
  mutate(name = recode(name, "pcr_il" = "Intl. Law", "pcr_ul" = "Ukr. Law"),
         region = recode(region, 
                         "East" = "Donbas territory controlled\nby the government",
                         "LNR/DNR" = "Donbas territory controlled\nby the separatists")) %>% 
  ggplot(aes(x = name, y = value, fill = "a"), width = .5) + 
  geom_bar(stat = "summary", fun = "mean", width = .7) + 
  stat_summary(fun.data = mean_se, fun.args = list(mult = 1), width=0.2, geom = "errorbar") +
  facet_wrap(~ region) + 
  theme_bw() + 
  theme(legend.position = "none") + 
  scale_fill_grey() + 
  labs(x = "Transitional Justice Outcome", 
       y = "Favorability toward transitional\njustice outcome (0 - 4)")
ggsave("figs/ukr_fig5.pdf", height = 5, width = 5)


# Outcome justice/peace -------------------------------------------------------


df %>% 
  select(ResponseId, pcr_il_p, pcr_il_j, pcr_ul_j, pcr_ul_p, 
         pcr_par_j, pcr_par_p, region) %>% 
  pivot_longer(-c(ResponseId, region)) %>% 
  mutate(outcome = case_when(str_detect(name, "ul") ~ "Ukr. Law",
                             str_detect(name, "il") ~ "Intl. Law",
                             str_detect(name, "par") ~ "Amnesty",
                             TRUE ~ NA_character_),
         mediator = case_when(str_detect(name, "_p$") ~ "Peace",
                              str_detect(name, "_j$") ~ "Justice",
                              TRUE ~ NA_character_),
         region = recode(region, "East" = "Donbas territory controlled\nby the government",
                         "LNR/DNR" = "Donbas territory controlled\nby the separatists"),
         value = recode(value, `0` = 0, `1` = 0, `2` = 1, `3` = 1, .default = NA_real_)
         ) %>% 
  ggplot(aes(x = outcome, y = value, fill = mediator)) + 
  stat_summary(fun.y = mean, geom = "bar", width = .5, position = "dodge") +
  stat_summary(fun.data = mean_se, geom = "errorbar", fun.args = list(mult = 1),
               width = .2, alpha = .8, position = position_dodge(width = 0.5)) +
  theme_bw() +
  scale_y_continuous(labels = scales::percent) + 
  facet_wrap(~region) + 
  coord_cartesian(ylim = c(0, 0.8)) + 
  labs(x = "Transitional justice outcome", y = "Percent who believe action will \nlead to justice or peace") + 
  theme_bw() + 
  scale_fill_grey() + 
  theme(legend.position = "bottom", legend.title = element_blank())
ggsave("figs/ukr_fig6.pdf", height = 5, width = 5)

# In-group vs. outgroup ------------------------------------------------------
df %>% pivot_longer(cols = c("pcr_par", "pcr_il", "pcr_ul")) %>% filter(pcr_mod != 2) %>%  
  mutate(name = recode(name, "pcr_par" = "Amnesty", "pcr_il" = "Intl. Law", "pcr_ul" = "Ukr. Law")) %>% 
  ggplot(aes(x = name, y = value, fill = in_out)) +
  stat_summary(fun = mean, geom = "bar", position = "dodge") +
  stat_summary(fun.data = mean_se, fun.args = list(mult = 1), geom = "errorbar", position = position_dodge(width = 0.9), width = 0.25) +
  scale_fill_grey() +
  theme_bw() + 
  labs(x = "", y = "Favorability of Transitional Justice Outcome\n(0-4)", subtitle = "Support for Transitional Justice Outcomes\nby Treatment Group") + 
  theme(legend.position = "bottom", legend.title = element_blank())
ggsave("figs/ukr_fig8.pdf", height = 4, width = 4)




# Revenge and fatigue -----------------------------------------------------
df %>% 
  mutate(fam_kill_perp = case_when(gov_kill == 1 & sep_kill == 1 ~ "...both sides",
                                   gov_kill == 1 ~ "...government forces",
                                   sep_kill == 1 ~ "...separatist forces",
                                   gov_kill == 0 & sep_kill == 0 ~ "No family killed",
                                   TRUE ~ NA_character_),
         fam_kill_perp = factor(fam_kill_perp, levels = c("...both sides", 
                                                          "...government forces", "...separatist forces",
                                                          "No family killed"))
  
  ) %>% pivot_longer(cols = c("pcr_par", "pcr_il", "pcr_ul")) %>%
  mutate(name = recode(name, "pcr_par" = "Amnesty", "pcr_il" = "Intl. Law", "pcr_ul" = "Ukr. Law")) %>% 
  rename(`Family Killed By...` = fam_kill_perp) %>% 
  filter(complete.cases(`Family Killed By...`)) %>% 
  ggplot(aes(x = name, y = value, fill = `Family Killed By...`)) +
  stat_summary(fun = mean, geom = "bar", position = "dodge") +
  stat_summary(fun.data = mean_se, fun.args = list(mult = 1), geom = "errorbar", position = position_dodge(width = 0.9), width = 0.25) +
  scale_fill_grey() +
  theme_bw() + 
  labs(x = "", y = "Favorability of Transitional Justice Outcome\n(0-4)", subtitle = "Support for Transitional Justice Outcomes by Exposure to Violence") + 
  theme(legend.position = "bottom")
ggsave("figs/ukr_fig7.pdf", height = 5, width = 7)

